package org.example.Other;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
 * @Description: TODO
 * @Author: sats@jz
 * @Date: 2025/1/13 14:02
 **/
public class PointUtil {
    private static final int WIDTH = 400;
    private static final int HEIGHT = 400;
    private static final int MIN_DISTANCE = 30;

    public static void main(String[] args) {
        List<Point> points = generateTrajectory(4);
        System.out.println("Generated Points: " + points);
    }

    private static List<Point> generateTrajectory(int numPoints) {
        List<Point> points = new ArrayList<>();
        Random random = new Random();

        while (points.size() < numPoints) {
            Point newPoint = new Point(random.nextInt(WIDTH), random.nextInt(HEIGHT));

            if (isValidPoint(newPoint, points)) {
                points.add(newPoint);
            }
        }

        return points;
    }

    private static boolean isValidPoint(Point point, List<Point> points) {
        // Check minimum distance with all existing points
        for (Point p : points) {
            if (point.distance(p) < MIN_DISTANCE) {
                return false;
            }
        }

        // Check angle condition if at least two points exist
        if (points.size() >= 2) {
            Point p1 = points.get(points.size() - 2);
            Point p2 = points.get(points.size() - 1);

            double angle = calculateAngle(p1, p2, point);
            if (Math.abs(angle - 180) < 10) { // Adjust threshold as needed
                return false;
            }
        }

        return true;
    }

    private static double calculateAngle(Point p1, Point p2, Point p3) {
        // Calculate vectors
        double v1x = p2.x - p1.x, v1y = p2.y - p1.y;
        double v2x = p3.x - p2.x, v2y = p3.y - p2.y;

        // Calculate dot product and magnitudes
        double dotProduct = v1x * v2x + v1y * v2y;
        double mag1 = Math.sqrt(v1x * v1x + v1y * v1y);
        double mag2 = Math.sqrt(v2x * v2x + v2y * v2y);

        // Return angle in degrees
        return Math.toDegrees(Math.acos(dotProduct / (mag1 * mag2)));
    }
}
